package com.eight.cloud.message.controller;

import com.eight.cloud.message.service.IMessageService;
import com.eight.cloud.core.global.result.Result;
import com.eight.cloud.core.global.result.ResultCode;
import com.eight.cloud.openfeign.message.model.MqMessage;
import com.eight.cloud.openfeign.message.model.SocketMessage;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 消息推送控制类
 *
 * @ClassName: PushMessageController
 * @Author: TXC
 * @Date: 2024-10-11 11:24
 **/
@RestController
@RequestMapping("/publish")
public class PushMessageController {
    @Resource
    private IMessageService messageService;

    /**
     * 发送websocket消息
     * @param socketMessage 消息
     */
    @PostMapping("/websocket")
    public Result<String> send(@RequestBody SocketMessage<?> socketMessage) {
        try {
            messageService.sendWebSocketMessage(socketMessage);
            return Result.ok();
        }catch (Exception e) {
            return Result.fail(ResultCode.ERROR, e.getMessage());
        }
    }

    /**
     * 发送mq消息
     * @param mqMessage 消息
     */
    @PostMapping("/mq")
    public Result<String> send(@RequestBody MqMessage<?> mqMessage) {
        try {
            boolean result = messageService.sendRabbitMessage(mqMessage);
            if (result) {
                return Result.ok();
            }
            return Result.fail(ResultCode.ERROR, "发送失败");
        }catch (Exception e) {
            return Result.fail(ResultCode.ERROR, e.getMessage());
        }
    }
}
